package model

import (
	"time"
)

/*
@Project        :   update_tools
@FileName       :   db_func.go
@Author         :   Administrator
@Datetime       :   2020/9/11 2:02
@Software       :   GoLand
@Show           :
*/

type LocalFile struct {
	Path        string `xml:"path,attr"`
	Url         string `xml:"url,attr"`
	Lastver     string `xml:"lastver,attr"`
	Size        int64  `xml:"size,attr"`
	NeedRestart string `xml:"needRestart,attr"`
	Zipped      string `xml:"zipped,attr"`
}

func ThanMd5(fileMd5 string, fileName string, serviceName string) string {
	db := DbInit()
	updateFile := UpdateFile{}
	db.Where("file_name = ? and mod_name = ?", fileName, serviceName).Last(&updateFile)
	if updateFile.FileMd5 == fileMd5 {
		return updateFile.Lastver
	} else {
		return "0"
	}
}

func CreateUpdate(localFile LocalFile, serviceName string, fileMd5 string) string {
	db := DbInit()
	createLog := UpdateFile{}
	createLog.FileName = localFile.Path
	createLog.Url = localFile.Url
	createLog.FileMd5 = fileMd5
	createLog.Lastver = time.Now().Format("2006.01.02.150405")
	createLog.Size = localFile.Size
	createLog.NeedRestart = localFile.NeedRestart
	createLog.Zipped = localFile.Zipped
	createLog.ModName = serviceName
	createLog.CreateTime = time.Now()
	db.Create(&createLog)
	return createLog.Lastver
}

func LastUpdateTime(serviceName string) string {
	var db = DbInit()
	updateFile := UpdateFile{}
	db.Where("mod_name = ?", serviceName).Last(&updateFile)
	UpdateTime := updateFile.CreateTime.Format("2006-01-02 15:04:05")
	if UpdateTime != "0001-01-01 00:00:00" {
		return UpdateTime
	} else {
		return "无更新记录"
	}
}
